(function() {
    var $ = require(L('$')), _ = require(L('_'));
    var enumObj = require(L('enumeration'));

    exports.countries = function(selectedValue) {
        var data = new Array();
        var countries = $.countries();
        _.each(countries, function(item) {
            data.push($.model('name_value')(item.Name, item.CountryID));
        });
        return exports.create('Select a country', data, selectedValue);
    };

    exports.gender = function(selectedValue) {
        var data = new Array();
        data.push($.model('name_value')('Female', 1));
        data.push($.model('name_value')('Male', 2));
        return exports.create('Select Gender', data, selectedValue);
    };

    exports.dob = function(selectedValue) {
        var data = new Array();
        for (var i = 1; i <= 31; i++) {
            data.push($.model('name_value')(i, i));
        };
        return exports.create('Day', data, selectedValue, 2);
    };

    exports.mob = function(selectedValue) {
        var data = new Array();
        for (var i = 1; i <= 12; i++) {
            data.push($.model('name_value')(i, i));
        };
        return exports.create('Month', data, selectedValue, 1);
    };

    exports.yob = function(selectedValue) {
        var date = new Date();
        var currentYear = date.getFullYear();
        var data = new Array();
        for (var i = currentYear; i > 1900; i--) {
            data.push($.model('name_value')(i, i));
        };
        return exports.create('Year', data, selectedValue, 2);
    };

    exports.sortBy = function(onChange) {
        /*var data = new Array();
         data.push($.model('name_value')('Newest first', 1));
         data.push($.model('name_value')('Oldest first', 2));
         data.push($.model('name_value')('Highest price', 3));
         data.push($.model('name_value')('Lowest price', 4));*/
        var data = enumObj.getSortTypeMyDesire();
        var style = {
            backgroundImage : L('bg_filter'),
            width : 142,
            height : 30,
            top : 0,
            paddingLeft : 7,
            textAlign : Titanium.UI.TEXT_ALIGNMENT_LEFT,
            font : {
                fontSize : '14dip',
                fontFamily : L('font_default')
            },
            color : '#4A4A4A',
            left : 0
        };
        return exports.create(enumObj.SortTypeMyDesire.SortBy.name, data, enumObj.SortTypeMyDesire.SortBy.value, 3, onChange, style);
    };
    exports.refine = function(onChange) {
        /*var data = new Array();
         data.push($.model('name_value')('Show all', 5));
         data.push($.model('name_value')('Live offers', 6));*/
        var data = enumObj.getRefineTypeMyDesire();
        var style = {
            backgroundImage : L('bg_filter'),
            width : 142,
            height : 30,
            top : 0,
            paddingLeft : 7,
            textAlign : Titanium.UI.TEXT_ALIGNMENT_LEFT,
            font : {
                fontSize : '14dip',
                fontFamily : L('font_default')
            },
            color : '#4A4A4A',
            right : 0
        };
        return exports.create(enumObj.RefineTypeMyDesire.Refine.name, data, enumObj.RefineTypeMyDesire.Refine.value, 3, onChange, style);
    };

    exports.sortByPurchase = function(onChange) {
        var data = enumObj.getSortTypePurchase();
        var style = {
            backgroundImage : L('bg_filter'),
            width : 142,
            height : 30,
            top : 0,
            paddingLeft : 7,
            textAlign : Titanium.UI.TEXT_ALIGNMENT_LEFT,
            font : {
                fontSize : '14dip',
                fontFamily : L('font_default')
            },
            color : '#4A4A4A',
            left : 0
        };
        return exports.create(enumObj.SortTypePurchase.SortBy.name, data, enumObj.SortTypePurchase.SortBy.value, 3, onChange, style);
    };
    exports.refinePurchase = function(newData, onChange) {
        var data = new Array();
        data.push($.model('name_value')(enumObj.RefineTypePurchase.ShowAll.name, enumObj.RefineTypePurchase.ShowAll.value));

        var newDataLength = newData.length;
        for (var i = 0; i < newDataLength; i++) {
            data.push(newData[i]);
        }

        var style = {
            backgroundImage : L('bg_filter'),
            width : 142,
            height : 30,
            top : 0,
            paddingLeft : 7,
            textAlign : Titanium.UI.TEXT_ALIGNMENT_LEFT,
            font : {
                fontSize : '14dip',
                fontFamily : L('font_default')
            },
            color : '#4A4A4A',
            right : 0
        };
        return exports.create(enumObj.RefineTypePurchase.Refine.name, data, enumObj.RefineTypePurchase.Refine.value, 3, onChange, style);
    };

    exports.refinePurchase.updateData = function() {
    };

    exports.create = function(title, nameValuePairs, selectedValue, size, onChange, style) {
        nameValuePairs = nameValuePairs || [];
        selectedValue = selectedValue || -1;
        title = title || 'Please select';
        var view;
        if (style) {
            var view = Ti.UI.createLabel(style);
        } else {
            size = size || 0;
            var width = '100%';
            left = 0;
            var bg = L('bg_ddl_large');
            if (size == 1) {
                width = 83;
                bg = L('bg_ddl_medium');
                left = 7;
            } else if (size == 2) {
                width = 67;
                bg = L('bg_ddl_small');
                left = 7;
            }
            view = Ti.UI.createLabel({
                backgroundImage : bg,
                width : width,
                height : 40,
                top : '7dip',
                paddingLeft : 7,
                textAlign : Titanium.UI.TEXT_ALIGNMENT_LEFT,
                font : {
                    fontSize : '14dip',
                    fontFamily : L('font_default')
                },
                color : '#4A4A4A',
                left : left
            });
        }

        view.setText = function(value) {
            view.text = '  ' + value;
        };

        view.updateSelectedValue = function(nameValuePairs) {
            if (nameValuePairs) {
                view.setText(nameValuePairs.name);
                view.selectedValue = nameValuePairs.value;
                
                var destructiveIndex = -1;
                var pairLength = view.nameValuePairs.length;
                for (var i = 0; i < pairLength; i++) {
                    if (view.nameValuePairs[i].value === nameValuePairs.value) {
                        destructiveIndex = i;
                        break;
                    }
                }
                view.optionDialog.destructive = destructiveIndex;
            }
        }
        var optionDialog = $.ui('dialog').create(title, nameValuePairs, selectedValue, function(e) {
            var defaultTitle = title;
            view.selectedText = e.name;
            view.selectedValue = e.value;
            view.setText(e.name);
            onChange && onChange(e.value);
        });
        view.optionDialog = optionDialog;
        view.addEventListener('click', function(e) {
            optionDialog.show();
        });

        view.nameValuePairs = nameValuePairs;

        view.val = function(value) {
            if ($.isNull(value))
                return view.selectedValue;
            for (var i = 0, j = view.nameValuePairs.length; i < j; i++) {
                if (view.nameValuePairs[i].value === value) {
                    var defaultTitle = title;
                    view.selectedText = view.nameValuePairs[i].name;
                    view.setText(view.nameValuePairs[i].name);
                    view.selectedValue = value;
                    break;
                }
            }
        };

        view.updateDataSource = function(nameValuePairs, selectedValue) {
            nameValuePairs = nameValuePairs || [];
            var selectedName = '';
            for (var i = 0, j = nameValuePairs.length; i < j; i++) {
                if (nameValuePairs[i].value === selectedValue) {
                    selectedName = nameValuePairs[i].name;
                    break;
                }
            }
            selectedName = $.isNull(selectedName) ? title : selectedName;
            view.setText(selectedName);
            view.dataSource = nameValuePairs;
            if (selectedName) {
                view.selectedText = selectedName;
                view.selectedValue = selectedValue;
            }
            view.optionDialog.updateDataSource(nameValuePairs, selectedValue);
        };
        view.updateDataSource(nameValuePairs, selectedValue);

        return view;
    };
})();
